<?php
/**
* QFSHOP 控制台首页
* =================================================================================================================================================
* 网络科技有限公司，并保留所有权利。
*/

define('IN_ZYS', true);
require(dirname(__FILE__) . '/includes/init.php');

//分类列表
if($_REQUEST['act'] == 'list'){
	$sql="select * from ".ly()."category order by sort_order";
	$row=$db -> getAll($sql);
	$type_list=type_list($row);
	$str=substr($type_list,0,strlen($type_list)-1);
	$arr=explode(',', $str);
	$type_list=quzhi($row,$arr);
	$smarty -> assign("cat_list",$type_list);
	$cat_list = cat_list(0, 0, false);
	$smarty -> assign("cat_list",$cat_list);
	$smarty -> display("category_list.html");
}elseif($_REQUEST['act'] == 'edit'){
	$c_id=isset($_REQUEST['c_id'])?$_REQUEST['c_id']:0;
	$p_id=isset($_REQUEST['p_id'])?$_REQUEST['p_id']:0;
	if($_POST){
		$cat_id=isset($_POST['cat_id'])?$_POST['cat_id']:0;//分类id
		$cat_name=isset($_POST['cat_name'])?$_POST['cat_name']:0;//分类名称
		$parent_id=isset($_POST['parent_id'])?$_POST['parent_id']:0;//上级分类id
		$is_show=isset($_POST['is_show'])?$_POST['is_show']:0;//是否显示
		$sort_order=isset($_POST['sort_order'])?$_POST['sort_order']:0;//排序号
		$cat_key=isset($_POST['cat_key'])?$_POST['cat_key']:0;//关键词
		$cat_desc=isset($_POST['cat_desc'])?$_POST['cat_desc']:0;//描述
		if($cat_id>0){
			$sql="update ".ly()."category set cat_name='{$cat_name}',parent_id='{$parent_id}',is_show='{$is_show}',sort_order='{$sort_order}',keywords='{$cat_key}',cat_desc='{$cat_desc}' where cat_id='{$cat_id}'";
		}else{
			$sql="insert into ".ly()."category(cat_name,parent_id,is_show,sort_order,keywords,cat_desc) values('{$cat_name}','{$parent_id}','{$is_show}','{$sort_order}','{$cat_key}','{$cat_desc}')";
		}
		$rs=$db -> query($sql);
		if($rs){
			$href[] = array('text' => "返回列表", 'href' => 'category.php?act=list');
			if($cat_id){$href[] = array('text' => "重新编辑", 'href' => 'category.php?act=edit&c_id='.$cat_id);}
			sys_msg("分类操作成功！",$href);
		}else{
			sys_msg("操作分类失败！");
		}
	}else{
		if($c_id){
			$sql="select * from ".ly()."category where cat_id='{$c_id}'";
			$data=$db -> getRow($sql);
			$parent_id=$data['parent_id'];
			$smarty -> assign("cat_info",$data);
			$smarty->assign('cat_type',cat_list(0,$parent_id));
		}elseif($p_id){
			$smarty->assign('cat_type',cat_list(0,$p_id));
		}else{
			$smarty->assign('cat_type',cat_list());
		}
		$smarty -> display("category_edit.html");
	}
}elseif($_REQUEST['act'] == 'ajax_del'){
	//删除商品分类
    $cat_id=isset($_POST['cat_id'])?$_POST['cat_id']:0;
    $sql="select cat_id from ".ly()."category where parent_id='{$cat_id}'";
    $res=$db->getAll($sql);
    if($res){
    	echo "3";   
    }else{
    	$sql="select * from ".ly()."goods where cat_id='{$cat_id}'";
    	$goods=$db->getAll($sql);
    	if($goods){
    		echo "3";
    	}else{
    		 $sql="delete from ".ly()."category where cat_id='{$cat_id}'";
	         $db->query($sql);
	         if(@mysql_affected_rows()){
	            echo "1";
	         }else{
	            echo "2";
	         }
    	}    
    }
}elseif($_REQUEST['act'] == 'sort'){
	//商品分类的排序
	$cat_id=isset($_POST['cat_id'])?$_POST['cat_id']:0;
	$sort=isset($_POST['val'])?$_POST['val']:0;
	$sql="update ".ly()."category set sort_order='{$sort}' where cat_id='{$cat_id}'";
	$res=$db -> query($sql);
	if($res){
		echo "1";	
	}else{
		echo "2";
	}
}elseif($_REQUEST['act'] == 'ajax_show'){
	//显示状态
    $cat_id=isset($_POST['cat_id'])?$_POST['cat_id']:0;
    $is_show=isset($_POST['zt'])?$_POST['zt']:0;
    if($is_show==1){$zt='0';}else{$zt='1';}
    $sql="update ".ly()."category set is_show='{$zt}' where cat_id='{$cat_id}'";
    $res=$db->query($sql);
    if($res){
        echo "1";   
    }else{
        echo "2";
    }
}


//查询所有分类的ID
function type_list($rows){
	$data=array();
	foreach ($rows as $key => $value) {
		if($value['parent_id']==0){
			$data[]=$value;
		}
	}
	$str='';
	$length=count($data);
	for ($i=0; $i < $length; $i++) { 
		$str.=$data[$i]['cat_id'].',';
		$str.=type_zi($rows,$data[$i]['cat_id']);
	}
	return $str;
}
//查询所有的子分类ID
function type_zi($data,$parent_id=0){
	$str='';
	foreach ($data as $key => $value) {
	    if($value['parent_id']==$parent_id){
	      	$str.=$value['cat_id'].',';
	      	$str.=type_zi($data,$value['cat_id']);
	     }
	}
	return $str;
}
//所有商品分类取值为一维数组
function quzhi($rows,$arr){
	$newArr=array();
	$length=count($arr);
	for ($i=0; $i <$length ; $i++) { 
		foreach ($rows as $key => $value) {
			if($value['cat_id']==$arr[$i]){
				$newArr[]=$value;	
			}
		}
	}
	return $newArr;
}
?>
